package yxy.game.pm2.command.hero;

import com.alibaba.fastjson.JSONArray;
import yxy.cherry.battle.module.Position;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Format;
import yxy.game.pm2.bean.Format.FormatType;
import yxy.game.pm2.bean.Hero;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.FormatMessage;
import yxy.game.pm2.module.hero.FormatHandle;
import yxy.game.pm2.module.hero.HeroHandle;
import yxy.game.pm2.module.niubee.NiuBeeModule;

import java.util.Collection;
import java.util.Date;
import java.util.Map;

/**
 * 布阵 获取布阵
 */
@MessageType(proto = Command.FormatInfo, classtype = FormatMessage.FormatInfoRequest.class)
public class CMD_FormatInfo extends Command<FormatMessage.FormatInfoRequest> {

    @Override
    public void execute(MessageSource messageSource, FormatMessage.FormatInfoRequest request) {
        Date date = new Date();

        try {
            String playerCode = messageSource.getTag();
            int typeNumber = request.getType().getNumber();
            FormatMessage.FormatInfoResponse.Builder response = FormatMessage.FormatInfoResponse.newBuilder();

            FormatType type = FormatType.forNumber(typeNumber);
            if (type == null) {
                send(messageSource, response, Result.ParamError);
                return;
            }

            HeroHandle module = new HeroHandle();
            Collection<Hero> heros = module.getHeros(playerCode);
            for (Hero hero : heros) {
                try {
                    response.addHeros(module.getHeroSimple(playerCode, hero));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            FormatHandle formatModule = new FormatHandle();
            response.setFormat(formatModule.get(formatModule.get(playerCode, type)));

            NiuBeeModule niubeeModule = NiuBeeModule.get(playerCode);
            response.setNbAct(niubeeModule.getProgress());
            response.setNbLevel(niubeeModule.getLevel());
            response.setNbSkill(niubeeModule.getSkillLevel());

            //返回上阵队伍的总战力
            Map<Position, Hero> map = new FormatHandle().formatHeros(playerCode, FormatType.普通);
            Format format = Format.get(playerCode, FormatType.普通);
            JSONArray jsonArray = new HeroHandle().getAllPower(playerCode, map, format,1);
            response.setHerosJsonArrary(jsonArray.toString());

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Date date1 = new Date();
        System.out.println("CMD_FormatInfo:" + (date1.getTime() - date.getTime()));
    }

    private void send(MessageSource messageSource, FormatMessage.FormatInfoResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}